package org.esti.model.utils;

import org.esti.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;

import java.beans.PropertyEditorSupport;

/**
 * @author Artem Loginov (logart)
 */

/**
 * Property editors used for convert string to not standard type.
 * This editor is used to convert level from id to Level object
 */
public class LevelPropertyEditor extends PropertyEditorSupport {

    @Autowired
    private final LevelService levelService;

    /**
     * @param levelService service to get access to level object by id
     */
    public LevelPropertyEditor(LevelService levelService) {
        this.levelService = levelService;
    }

    /**
     * recognize level using levelId and pass it to binder,
     *
     * @param levelId id of level which will be recognized
     */
    @Override
    public void setAsText(String levelId) {
        Long id = Long.parseLong(levelId);
        super.setValue(levelService.getLevel(id));
    }

}